#include "historywindow.h"
#include "QIcon"
#include "QVBoxLayout"
#include "QScrollArea"
#include "QLabel"
#include "main.h"


historywindow::historywindow(QString object_userid,QWidget* parent):
    QWidget(parent)
{
    this->setWindowTitle("IM_software history");
    this->setWindowIcon(QIcon(":/new/prefix1/images/icons_head.png"));
    this->setFixedSize(700,600);
    QWidget *scrollareacontent;//滚动框的内容页面
    QScrollArea *scrollarea;//滚动框的区域
    QWidget* page = new QWidget(this);//滚动框的总页面

    qDebug()<<"1111";
    //page在page_friends平铺
    QVBoxLayout *m_layout = new QVBoxLayout(this);
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setSpacing(0);
    m_layout->addWidget(page);
    this->setLayout(m_layout);

    qDebug()<<"2222";
    //设置带滚动区域和滚动内容界面
    scrollarea = new QScrollArea(page);
    scrollarea->setWidgetResizable(true);
    scrollarea->setGeometry(0,0,page->width(),page->height());
    scrollareacontent = new QWidget();
    scrollarea->setWidget(scrollareacontent);

    // 将 QScrollArea 放置在一个布局中，以便自动随窗口大小变化
    QVBoxLayout* scrollLayout = new QVBoxLayout();
    scrollLayout->addWidget(scrollarea);
    page->setLayout(scrollLayout);

    QVBoxLayout* playout = new QVBoxLayout(page);
    playout->setAlignment(Qt::AlignTop );//垂直靠上
    scrollareacontent->setLayout(playout);
    scrollareacontent->setStyleSheet("background-color: transparent;");


    qDebug()<<"333";
    //获得历史信息然后插入playout
    qDebug()<<object_userid;
    qDebug()<<object_userid[0];
    QVector<Message> msgs;
    if (object_userid[0] == "1"){
        qDebug()<<"444";
        msgs = db.GetSomeRecentMessageByid(object_userid.toInt(), use_uid.toInt(), 5000);
    }
    else
        msgs = db.GetSomeRecentMessageByGroupid(object_userid.toInt(), 5000);

    for(auto msg:msgs){//获取多条信息
        //获取信息

        qDebug()<<"444";
        //设置布局
        QWidget* all_page= new QWidget;
        QLabel* name = new QLabel ;
        QLabel* time= new QLabel;
        QLabel* content= new QLabel;
        QHBoxLayout* first = new QHBoxLayout;
        QHBoxLayout* sencond = new QHBoxLayout;
        QWidget* first_page = new QWidget;
        QWidget* sencond_page = new QWidget;
        QVBoxLayout* all = new QVBoxLayout;

        qDebug()<<"555";
        first->addWidget(name);
        first->addWidget(time);
        sencond->addWidget(content);
        first_page->setLayout(first);
        sencond_page->setLayout(sencond);

        all->addWidget(first_page);
        all->addWidget(sencond_page);
        all_page->setLayout(all);
//        all_page->setStyleSheet("QWidget#all_page {border: 2px solid #000000;}");

        qDebug()<<"666";
        //显示信息
        name->setText(userMap[QString::number(msg.tid)]->name);
        time->setText(msg.time.toString());
        content->setText(msg.msg);
        if(userMap[QString::number(msg.tid)]->name==use_name){
            name->setStyleSheet("color: #00FF00;");//绿色
            time->setStyleSheet("color: #00FF00;");
            qDebug()<<"jinru";
        }
        else{
            name->setStyleSheet("color: #FF0000;");//红色
            time->setStyleSheet("color: #FF0000;");//红色
            qDebug()<<"jinru2";
        }

        qDebug()<<"插入成功"<<userMap[QString::number(msg.tid)]->name<<msg.time.toString()<<msg.msg;
        playout->addWidget(all_page);

    }
    qDebug()<<"777";
    playout->addStretch();
    playout->setSpacing(0);
    qDebug()<<"888";
    this->setLayout(playout);
    this->show();
}

